Page({

  
  data: {
    prize_arr: [
      {id: '1',type: 'prize',name: '前卫南区',isSelected: false},
      {id: '2',type: 'prize',name: '南岭校区',isSelected: false},
      {id: '3',type: 'prize',name: '朝阳校区',isSelected: false},
      {id: '4',type: 'prize',name: '南湖校区',isSelected: false},
      {id: '5',type: 'prize',name: '新民校区',isSelected: false},
      {id: '6',type: 'prize',name: '和平校区',isSelected: false},
      {id: '7',type: 'block',name: '----------',isSelected: false},
      {id: '8',type: 'btn',name: '点我开始',isSelected: false},
      {id: '9',type: 'block',name: '----------',isSelected: false}
    ],
    
    num_interval_arr: [40, 50, 60, 70, 80, 90, 100, 120, 150, 180],
   
    order_arr: [0, 1, 2, 3, 4, 5],
    //是否转完了
    isTurnOver: true,
    // 是否需要复原，把没转完的圈转完
    isRecover: false,
    // 记录上一次id
    prize_id_last: ''
  },

  // 点击开始
  clickPrize() {
    // 如果不在抽取状态中，则执行动画
    if (this.data.isTurnOver) {
      // 把抽取状态改为未完成
      this.setData({
        isTurnOver: false
      })
      // 抽取后返回的ID
      let prize_id = Math.random()*6+1;
      
      //调用
      this.lottery(prize_id);
    } else {
      wx.showToast({
        title: '请勿重复点击',
        icon: 'none'
      })
    }
  },

  //旋转动画
  async lottery(prize_id) {
    console.log('中奖ID：' + prize_id)
    // 如果不是第一次，需要等待上一圈没跑完的次数跑完再执行
    this.recover().then(() => {
      let num_interval_arr = this.data.num_interval_arr;
      let order_arr = this.data.order_arr;
      // 旋转总次数
      let sum_rotate = num_interval_arr.length;
      // 每一圈所需要的时间
      let interval = 0;
      num_interval_arr.forEach((delay, index) => {
        setTimeout(() => {
          this.rotateCircle(delay, index + 1, sum_rotate, prize_id, order_arr);
        }, interval)
        //因为每一圈转完所用的时间是不一样的，所以要做一个叠加操作
        interval += delay * 8;
      })
    })
  },

  
  rotateCircle(delay, index, sum_rotate, prize_id, order_arr_pre) {
    let _this = this;
    
    let prize_arr = this.data.prize_arr;// 页面总数组
   
    let order_arr = [] // 执行顺序数组
   
    if (index == sum_rotate) {
      order_arr = order_arr_pre.slice(0, prize_id)
    } else {
      order_arr = order_arr_pre;
    }
    for (let i = 0; i < order_arr.length; i++) {
      setTimeout(() => {
        
        prize_arr.forEach(e => {
          e.isSelected = false
        })
       
        prize_arr[order_arr[i]].isSelected = true;
        
        _this.setData({// 更新状态
          prize_arr: prize_arr
        })
        
        if (index === sum_rotate && i === order_arr.length - 1 && !this.data.isRecover) {
          _this.setData({
            isTurnOver: true,
            isRecover: true,
            prize_id_last: prize_id
          })
        }
      }, delay * i)
    }
  },

  
  async recover() {                // 复原
    if (this.data.isRecover) {      // 判断是否需要重置
      let delay = this.data.num_interval_arr[0]; 
      let order_arr = this.data.order_arr;
      let prize_id_last = this.data.prize_id_last;    // 上一次的id
      order_arr = order_arr.slice(prize_id_last);       // 截取未跑完的格子
      return await new Promise(resolve => {    // 确保跑完后才去执行新的
        this.rotateCircle(delay, 1, 1, 8, order_arr); 
        setTimeout(() => {                   // 确保跑完后，不用重置
          this.setData({
            isRecover: false,
          })
          resolve() 
        }, delay * order_arr.length)
      })
    }
  }

})
